package maa.action.all;

import maa.action.Action;
import maa.action.ActionResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ChangeLocale implements Action {
    @Override
    public ActionResult execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        ActionResult result = new ActionResult();
        String locale = req.getParameter("locale");
        String page = getPageName(req);
        HttpSession session = req.getSession();
        session.setAttribute("language",locale);
        result.setResult(page);
        result.setRedirected(true);
        return result;
    }

    private String getPageName(HttpServletRequest request) {
        String string = request.getHeader("referer");
        int k = string.indexOf('=');
        return string.substring(k+1,string.length());
    }
}
